let proto = {
  onerror(err) {
    console.log("中间件层捕获函数");

    this.app.emit("error", err);
    // 中间层捕获异常后, 需要响应
    this.status = 500;
    let msg = "服务器内部错误";
    this.res.setHeader("Content-Type", "text/plain; charset=utf-8");
    this.res.setHeader("Content-Length", Buffer.byteLength(msg));
    this.res.end(msg);
  },
};

/**
 * 用于获取对象上的属性
 * @param {object} property
 * @param {string} name
 */
function delegateGet(property, name) {
  proto.__defineGetter__(name, function () {
    return this[property][name];
  });
}

/**
 * 用于挂载对象上的属性和值
 * @param {object} property
 * @param {string} name
 */
function delegateSet(property, name) {
  proto.__defineSetter__(name, function (val) {
    this[property][name] = val;
  });
}

let requestGet = ["query"];
let requestSet = [];

let responseGet = ["body", "status"];
let responseSet = responseGet;

requestGet.forEach((ele) => delegateGet("request", ele));
requestSet.forEach((ele) => delegateSet("request", ele));
responseGet.forEach((ele) => delegateGet("response", ele));
responseSet.forEach((ele) => delegateSet("response", ele));

module.exports = proto;
